﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using wfDesigner.Model;
using System.Windows.Input;
using Microsoft.Expression.Interactivity.Core;
using Microhard.WorkflowDataAccess;

namespace wfDesigner.ViewModel
{
    public class FlowChartViewModel : INotifyPropertyChanged 
    {
        public FlowChartViewModel()
        {
            this.DeleteCommand = new ActionCommand(this.ExecuteDelete);
           
        }


        #region 属性定义
        //记录总数
        public int _total;
        public int Total
        {
            get
            {
                return _total;
            }
            set
            {
                _total = value;
                if (PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs("Total"));
                }
            }
        }

        //第几页
        public int _pageIndex = 1;
        public int PageIndex
        {
            get
            {
                return _pageIndex;
            }
            set
            {
                _pageIndex = value;
                if (PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs("PageIndex"));
                }
            }
        }

        //每页显示记录数
        public int _pageSize = 1;
        public int PageSize
        {
            get
            {
                return _pageSize;
            }
            set
            {
                _pageSize = value;
                if (PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs("PageSize"));
                }
            }
        }


        //public int _SelIndex;
        //public int SelIndex
        //{
        //    get
        //    {
        //        return _SelIndex;
        //    }
        //    set
        //    {
        //        _SelIndex = value;
        //        if (PropertyChanged != null)
        //        {
        //            this.PropertyChanged(this, new PropertyChangedEventArgs("SelIndex"));
        //        }
        //    }
        //}

        public FlowChart _selItem;
        public FlowChart SelItem
        {
            get
            {
                return _selItem;
            }
            set
            {
                _selItem = value;
                if (PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs("SelItem"));
                }
            }
        }

        private List<FlowChart> _flowCharList;
        public List<FlowChart> FlowCharList
        {
            get
            {
                return _flowCharList;
            }
            set
            {
                _flowCharList = value;
                NotifyPropertyChanged("FlowCharList");
            }
        }

        public List<FlowChart> FlowCharRecords { get; set; }

        public ICommand DeleteCommand
        {
            get;
            private set;
        }

 
        #endregion

        #region 方法定义
        public void Query(int pageIndex)
        {
            int id = (pageIndex - 1) * _pageSize + 1;
            WorkflowDBDataContext entity = new WorkflowDBDataContext();
            var flowcharList1 = from p in entity.t_FlowCharts select p;

           
           List<FlowChart> flowcharList = new List<FlowChart>();

           foreach (var q in flowcharList1)
           {
               FlowChart f = new FlowChart();
               f.FlowChartID = q.FlowChartId;
               f.FlowChartName = q.FlowChartName;
               f.Creator = q.Creator;
               f.ResourceName = q.ResourceName;
               f.OperationName = q.OperationName;
               f.TypeName = GetFlowTypeName(new Guid(q.TypeId.ToString()));
               f.Version = q.Version;
               if (q.Status == 0)
               { f.Status = "关闭"; }
               else
               {f.Status="已发布";}
               f.CreateDate = q.CreateDate.ToString();

               flowcharList.Add(f);
           }

         
           Total = flowcharList.Count();

           if (Total > 0)
           {
                List<FlowChart>  total = flowcharList.OrderBy(n => n.CreateDate).Skip((pageIndex - 1) * _pageSize).Take(_pageSize).ToList();

                foreach (FlowChart t in total)
               {
                   t.ID = id;
                   id++;
               }
                this.FlowCharList = total;
           }
           else
           {
               this.FlowCharList = null;
           }

        }

       
        private string GetFlowTypeName(Guid typeid)
        {
            WorkflowDBDataContext entity = new WorkflowDBDataContext();
          var t= entity.t_WorkFlowTypes.Where(n => n.TypeId == typeid).SingleOrDefault();
          return t.TypeName;
        }

        private void ExecuteDelete()
        {
            WorkflowDBDataContext entity = new WorkflowDBDataContext();
            if (this.SelItem.FlowChartID !=new Guid( "00000000-0000-0000-0000-000000000000"))
            {
                var f = entity.t_FlowCharts.Where(n => n.FlowChartId == SelItem.FlowChartID).SingleOrDefault();
                entity.t_FlowCharts.DeleteOnSubmit(f);
                entity.SubmitChanges();
            }
           
            Query(1);
        }

    

     
        #endregion



        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion

    }
}
